package com.kly.cashmall.module.profile.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.kly.cashmall.R;
import com.kly.cashmall.widget.GetText;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author zxp
 * @since 27/10/21
 */
public class GenericSimpleAdapter <T> extends BaseAdapter {
    private Context mContext;
    private List<T> mBeans = new ArrayList<>();
    private int selectedIndex;

    private GetText<T> getText;

    public GenericSimpleAdapter(Context mContext, List<T> mBeans, int selectedIndex) {
        this.mContext = mContext;
        this.mBeans = mBeans;
        this.selectedIndex = selectedIndex;
    }

    public GenericSimpleAdapter setGetText(GetText<T> getText) {
        this.getText = getText;
        return this;
    }

    @Override public int getCount() {
        if (mBeans != null) {
            return mBeans.size();
        }
        return 0;
    }

    @Override public Object getItem(int position) {
        if (mBeans != null) {
            return mBeans.get(position);
        }
        return null;
    }

    @Override public long getItemId(int position) {
        return position;
    }

    @Override public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder;
        if (convertView == null) {
            convertView =
                    LayoutInflater.from(mContext).inflate(R.layout.item_simple_text_item, parent, false);
            mHolder = new ViewHolder();
            mHolder.mTextView = (TextView) convertView.findViewById(R.id.simple_text);
            mHolder.mBottomLine = convertView.findViewById(R.id.bottom_line);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        if (!isEmpty(mBeans)) {

            if (getText != null) {
                mHolder.mTextView.setText(getText.toText(mBeans.get(position)));
            } else {
                mHolder.mTextView.setText(mBeans.get(position).toString());
            }
        }
        if (position == mBeans.size() -1) {
            mHolder.mBottomLine.setVisibility(View.GONE);
        }else {
            mHolder.mBottomLine.setVisibility(View.VISIBLE);
        }
        if (!isEmpty(mBeans)) {
            if (position == selectedIndex) {
                mHolder.mTextView.setTextColor(mContext.getResources().getColor(R.color.main));
            } else {
                mHolder.mTextView.setTextColor(mContext.getResources().getColor(R.color.color_4c4c4c));
            }
        }
        return convertView;
    }

    private static class ViewHolder {
        TextView mTextView;
        View mBottomLine;
    }

    public void setSelectedIndex(int index) {
        this.selectedIndex = index;
        notifyDataSetChanged();
    }

    public void clear() {
        mBeans.clear();
        notifyDataSetChanged();
    }

    public void addAll(List<T> items) {
        mBeans.addAll(items);
        notifyDataSetChanged();
    }

    private boolean isEmpty(Collection<?> t) {
        return null == t || t.size() < 1;
    }

    public int getSelectedIndex() {
        return selectedIndex;
    }
}
